package com.sw.small_world.fragment.me;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.activity.ClickButtonActivity;
import com.sw.small_world.fragment.base.BaseFragment;
import com.sw.small_world.utils.ConstantUtils;
import com.sw.small_world.widgets.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/16.
 */

public class MyFriendsFragment extends BaseFragment {

    @Bind(R.id.search)
    EditText search;
    @Bind(R.id.list_view)
    ExpandableListView listView;
    private ClickButtonActivity activity;
    ArrayList<String> groups = new ArrayList<>();
    ArrayList<String> children = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (ClickButtonActivity) getActivity();
        activity.tvTitle.setText("我的人脉");
    }

    @Override
    protected Object requestData() {
        for (int i = 0; i < 10; i++) {
            groups.add("");
            if (i % 3 == 0) {
                children.add("");
            }
        }
        return ConstantUtils.STATE_SUCCESSED;
    }

    @Override
    protected View getSuccessView() {
        View view = View.inflate(activity, R.layout.fragment_my_friends, null);
        ButterKnife.bind(this, view);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setGroupIndicator(null);// 去掉默认箭头
        for (int i = 0; i < groups.size(); i++) {// 默认全部展开
           listView.expandGroup(i);
        }
        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {// 点击不能收缩
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });

        return view;
    }

    @Override
    public void onClick(View v) {

    }

    class MyAdapter implements ExpandableListAdapter {


        @Override
        public int getGroupCount() {
            return groups.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return children.size();
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_my_friends_group, null);
                holder = new GViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (GViewHolder) convertView.getTag();
            }
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_my_friends_child, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public void onGroupExpanded(int groupPosition) {

        }

        @Override
        public void onGroupCollapsed(int groupPosition) {

        }

        @Override
        public long getCombinedChildId(long groupId, long childId) {
            return 0;
        }

        @Override
        public long getCombinedGroupId(long groupId) {
            return 0;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        class GViewHolder {

            @Bind(R.id.tv_group)
            TextView tvGroup;

            GViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

        class ViewHolder {
            @Bind(R.id.iv_portrait)
            CircleImageView ivPortrait;
            @Bind(R.id.tv_name)
            TextView tvName;
            @Bind(R.id.tv_deep)
            TextView tvDeep;
            @Bind(R.id.tv_company_position)
            TextView tvCompanyPosition;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
